package com.masterclient.app.modules.income.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;


public class IncomerSecondUpdateDialog extends Dialog {
    String title;
    String cancel;
    String confirm;
    int type;
    Context context;
    public IncomerSecondUpdateDialog(@NonNull Context context, String title, String cancel, String confirm) {
        super(context,R.style.MyDialog);
        this.title = title;
        this.cancel = cancel;
        this.confirm = confirm;
        this.context = context;
    }

    public IncomerSecondUpdateDialog(@NonNull Context context, String title, String cancel, String confirm, int type) {
        super(context,R.style.MyDialog);
        this.title = title;
        this.cancel = cancel;
        this.confirm = confirm;
        this.type = type;
        this.context = context;
    }

    private TextView tv_title,tv_cancel,tv_confirm,get_code_btn,sms_mobile_txt;
    View line,close;
    EditText et_phone,code_input;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_incomer_second_update);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        get_code_btn = (TextView) findViewById(R.id.get_code_btn);
        sms_mobile_txt = (TextView) findViewById(R.id.sms_mobile_txt);
        et_phone = (EditText) findViewById(R.id.et_phone);
        code_input = (EditText) findViewById(R.id.code_input);
        line = findViewById(R.id.line);
        close = findViewById(R.id.close);
//        switch (type){
//            case 1:
//                tv_cancel.setVisibility(View.GONE);
//                line.setVisibility(View.GONE);
//                tv_confirm.setBackgroundResource(R.drawable.cash_common_dialog_confirm1_bkg);
//                break;
//        }

        tv_title.setText(title);
        tv_cancel.setText(cancel);
        tv_confirm.setText(confirm);
        sms_mobile_txt.setText("验证码将发送至 " + sms_mobile);

        tv_cancel.setOnClickListener(v->{
            if(onChooseListerner!= null){
                onChooseListerner.cancel();
            }
        });
        tv_confirm.setOnClickListener(v->{
            if(onChooseListerner!= null){
                if(TextUtils.isEmpty(et_phone.getText().toString().trim())){
                    Toast.makeText(context,"手机号输入错误，请重新输入",Toast.LENGTH_LONG).show();
                    return;
                }
                if(TextUtils.isEmpty(code_input.getText().toString().trim())){
                    Toast.makeText(context,"请输入验证码",Toast.LENGTH_LONG).show();
                    return;
                }
                onChooseListerner.confirm(et_phone.getText().toString().trim(),code_input.getText().toString().trim());
            }
        });
        close.setOnClickListener(v->{
            if(onChooseListerner!= null){
                onChooseListerner.cancel();
            }
        });
        get_code_btn.setOnClickListener(v->{
            if(TextUtils.isEmpty(et_phone.getText().toString().trim())){
                Toast.makeText(context,"手机号输入错误，请重新输入",Toast.LENGTH_LONG).show();
                return;
            }
            boolean canGetCode = "获取验证码".equals(get_code_btn.getText().toString().trim()) | "重新获取".equals(get_code_btn.getText().toString().trim());
            if(onChooseListerner!= null && canGetCode){
                onChooseListerner.get_code(et_phone.getText().toString().trim());
            }
        });

    }

    public void setOneButton(){
        tv_cancel.setVisibility(View.GONE);
        line.setVisibility(View.GONE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    public void setOnChooseListerner(OnChooseListerner onChooseListerner) {
        this.onChooseListerner = onChooseListerner;
    }

    private OnChooseListerner onChooseListerner;

    public interface OnChooseListerner{
        public void cancel();
        public void confirm(String mobile,String code);
        public void get_code(String mobile);
    }

    CountDownTimer timer;
    public void setTime(){

        get_code_btn.setTextColor(0xffc1c1c1);
        timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                get_code_btn.setText(millisUntilFinished / 1000 + "s");
                //get_code_btn.setTextColor(0x4d000000);
                //tv_get_yan.setBackgroundResource(R.drawable.withdraw_get_code_bkg);
            }

            @Override
            public void onFinish() {
                get_code_btn.setTextColor(0xfff23051);
                get_code_btn.setBackground(null);
                get_code_btn.setText("重新获取");
            }
        };
        timer.start();

    }

    String sms_mobile;
    public void set_sms_mobile(String sms_mobile){
        this.sms_mobile = sms_mobile;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if(timer != null){
            timer.cancel();
        }
    }
}
